package com.cssy.xxkj.logic.web;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.xxkj.logic.dao.LogicMaterialBackItemDAO;
import com.cssy.xxkj.logic.entity.LogicMaterialBackDO;
import com.cssy.xxkj.logic.entity.LogicMaterialBackItemDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseDetailsDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseInventoryDO;
import com.cssy.xxkj.logic.service.ILogicMaterialBackService;
import com.cssy.xxkj.logic.service.ILogicWarehouseDetailsService;
import com.cssy.xxkj.logic.service.ILogicWarehouseInventoryService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.util.SessionUtil;

@Controller
@RequestMapping("/modify")
public class ModifyDataController {
	
	@Resource(name = "logicWarehouseDetailsService")
	private ILogicWarehouseDetailsService logicWarehouseDetailsService;
	
	@Resource(name = "logicWarehouseInventoryService")
	private ILogicWarehouseInventoryService logicWarehouseInventoryService;
	
	@Resource(name="logicMaterialBackService")
	private ILogicMaterialBackService logicMaterialBackService;
	
	@Resource(name = "logicMaterialBackItemDAO")
	private LogicMaterialBackItemDAO logicMaterialBackItemDAO;
	
	@RequestMapping("/bitchin")
	public void modifyBitchIn(HttpServletRequest request){
		SearchConditions scs=new SearchConditions();
		//获取退货信息
		scs.addCondition("zd002Type", "BI");
		scs.addCondition("zd003Statue", "MRCK_AUDIT");
		scs.setLimitClause(null);
		@SuppressWarnings("unchecked")
		List<LogicMaterialBackDO> mbList=this.logicMaterialBackService.findListByExample(scs).getResults();
		Map<String, LogicMaterialBackItemDO> mbMap= new HashMap<String, LogicMaterialBackItemDO>();
		for(LogicMaterialBackDO mb:mbList){
			LogicMaterialBackItemDO logicMaterialBackItemDO=new LogicMaterialBackItemDO();
			logicMaterialBackItemDO.setZd001BackId(mb.getId());
			List<LogicMaterialBackItemDO> itemList=this.logicMaterialBackItemDAO.findListByExample(logicMaterialBackItemDO);
			if(itemList!=null){
				for(LogicMaterialBackItemDO item:itemList){
					//warehouseCode  piCode mcode mname mspec mmodel mcolor mquanlity
					String key=mb.getZd006WhCode()+"_"+item.getZd012PiCode()+"_"+
									(StringUtils.isEmpty(item.getZd002MaterCode())?null:item.getZd002MaterCode())+"_"+
									(StringUtils.isEmpty(item.getZd002MaterName())?null:item.getZd002MaterName())+"_"+
									(StringUtils.isEmpty(item.getZd002MaterSpec())?null:item.getZd002MaterSpec())+"_"+
									(StringUtils.isEmpty(item.getZd002MaterModel())?null:item.getZd002MaterModel())+"_"+
									(StringUtils.isEmpty(item.getZd002MaterColor())?null:item.getZd002MaterColor())+"_"+
									(StringUtils.isEmpty(item.getZd002MaterQuanlity())?null:item.getZd002MaterQuanlity());
					mbMap.put(key, item);
				}
			}
		}
		//获取所有退货记录
		scs.clearCondition();
		scs.addCondition("zd002BillType", "RG");
		scs.setLimitClause(null);
		@SuppressWarnings("unchecked")
		List<LogicWarehouseDetailsDO> listDetails=this.logicWarehouseDetailsService.findListByExample(scs).getResults();
		Map<String, LogicWarehouseDetailsDO> detailsMap=new HashMap<String ,LogicWarehouseDetailsDO>();
		for(LogicWarehouseDetailsDO de:listDetails){
			//warehouseCode  piCode mcode mname mspec mmodel mcolor mquanlity
			String key=de.getZd003WhCode()+"_"+de.getZd020PiCode()+"_"+
						(StringUtils.isEmpty(de.getZd005MaterialsCode())?null:de.getZd005MaterialsCode())+"_"+
						(StringUtils.isEmpty(de.getZd005MaterialsName())?null:de.getZd005MaterialsName())+"_"+
						(StringUtils.isEmpty(de.getZd006MaterialsSpec())?null:de.getZd006MaterialsSpec())+"_"+
						(StringUtils.isEmpty(de.getZd007MaterialsModel())?null:de.getZd007MaterialsModel())+"_"+
						(StringUtils.isEmpty(de.getZd008MaterialsColor())?null:de.getZd008MaterialsColor())+"_"+
						(StringUtils.isEmpty(de.getZd008MaterialsQuanlity())?null:de.getZd008MaterialsQuanlity());
			if(mbMap.get(key)!=null
					&&mbMap.get(key).getZd001BackCode().equals(de.getZd001BillCode())){
				LogicWarehouseDetailsDO lw= detailsMap.get(key);
				if(lw==null){
					lw=new LogicWarehouseDetailsDO();
				}
				int rlength=de.getZd001BillCode().replace("150400", "").length();
				int nlength=de.getZd001BillCode().length();
				if(rlength==nlength){
					continue;
				}
				//以前的退货数
				BigDecimal bhsback=lw.getZd011BasicQuantity()==null?BigDecimal.ZERO:lw.getZd011BasicQuantity();
				//当前的退货数
				BigDecimal nhsback=de.getZd011BasicQuantity()==null?BigDecimal.ZERO:de.getZd011BasicQuantity();
				if(nhsback.compareTo(BigDecimal.ZERO)==-1){
					continue;
				}
				de.setZd011BasicQuantity(bhsback.add(nhsback));
				detailsMap.put(key, de);
				de.setZd011BasicQuantity(BigDecimal.ZERO.subtract(de.getZd011BasicQuantity().abs()));
				logicWarehouseDetailsService.update(de, SessionUtil.getUser(request));
			}
		}
		//获取现在的库存
		scs.clearCondition();
		scs.setLimitClause(null);
		@SuppressWarnings("unchecked")
		List<LogicWarehouseInventoryDO> listInventory=this.logicWarehouseInventoryService.findListByExample(scs).getResults();
		Map<String, LogicWarehouseInventoryDO> inMap=new HashMap<String, LogicWarehouseInventoryDO>();
		for(LogicWarehouseInventoryDO lw:listInventory){
			//warehouseCode  piCode mcode mname mspec mmodel mcolor mquanlity
			String key=lw.getZd001WhCode()+"_"+lw.getZd026PiCode()+"_"+
						(StringUtils.isEmpty(lw.getZd004MaterialsCode())?null:lw.getZd004MaterialsCode())+"_"+
						(StringUtils.isEmpty(lw.getZd005MaterialsName())?null:lw.getZd005MaterialsName())+"_"+
						(StringUtils.isEmpty(lw.getZd006MaterialsSpec())?null:lw.getZd006MaterialsSpec())+"_"+
						(StringUtils.isEmpty(lw.getZd007MaterialsModel())?null:lw.getZd007MaterialsModel())+"_"+
						(StringUtils.isEmpty(lw.getZd008MaterialsColor())?null:lw.getZd008MaterialsColor())+"_"+
						(StringUtils.isEmpty(lw.getZd008MaterialsQuanlity())?null:lw.getZd008MaterialsQuanlity());
			LogicWarehouseInventoryDO in=inMap.get(key);
			if(in!=null){
				lw.setZd009Quantity(in.getZd009Quantity().add(lw.getZd009Quantity()));
			}
			inMap.put(key, lw);
		}
		String msg="错误数据：仓库编码_pi单号_物料编码_名称_规格_型号_颜色_材质 ";
		for(String key:detailsMap.keySet()){
			//库存
			LogicWarehouseInventoryDO lwi=inMap.get(key);
			if(lwi==null){
				throw new SyBaseException("数据有误"+key);
			}
			LogicWarehouseDetailsDO lwd=detailsMap.get(key);
			if((lwi.getZd009Quantity()).compareTo(lwd.getZd011BasicQuantity().multiply(new BigDecimal(2)))==-1){
				msg=msg+","+key+"仓库名称："+lwi.getZd001WhName()+"反审数量:"+lwd.getZd011BasicQuantity()
						+"库存数量:"+lwi.getZd009Quantity();
				//FileOutputStream fos=new FileOutputStream();
			}else{
				lwi.setZd009Quantity(lwi.getZd009Quantity().add(lwd.getZd011BasicQuantity().multiply(new BigDecimal(2))));
				logicWarehouseInventoryService.update(lwi, SessionUtil.getUser(request));
			}
		}
		try {
			File file= new  File("D:/msg.txt");
			if(!file.exists()){
				file.createNewFile();
			}
			Writer writer = new OutputStreamWriter(new FileOutputStream(file, true));
			writer.write(msg);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
